package com.dearbinge.opentapi.content.commodity;

import com.dearbinge.data.api.commodity.CommodityCommissionService;
import com.dearbinge.data.pojo.commodity.vo.CommodityCommissionRequestVO;
import com.dearbinge.data.pojo.commodity.vo.CommodityCommissionResponseVO;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 商品佣金控制层
 *
 * @author wes
 * @date 2019/3/8
 */
@Api(tags = "2.3、商品-佣金")
@RestController
@CrossOrigin
@RequestMapping("/commodityCommission")
@EnableAutoConfiguration
public class CommodityCommissionController extends WebMvcConfigurerAdapter {

    @Autowired
    private CommodityCommissionService commodityCommissionService;


    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "commodityCommissionId", value = "商品佣金表id", paramType = "query",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer commodityCommissionId) {
        try {
            Integer dataCode = commodityCommissionService.delete(commodityCommissionId);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询对象",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<CommodityCommissionResponseVO> getById(Integer id){
        try {
            CommodityCommissionResponseVO userContactTag = commodityCommissionService.getById(id);
            return ResultMsg.ok(userContactTag);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody CommodityCommissionRequestVO commodityCommissionRequestVO){
        try {
            if (commodityCommissionRequestVO.getCommodityCommissionId() != null && commodityCommissionRequestVO.getCommodityCommissionId() != 0){
                Integer dataCode = commodityCommissionService.update(commodityCommissionRequestVO);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else{
                Integer dataCode = commodityCommissionService.insert(commodityCommissionRequestVO);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


}
